package com.bbcloud.controller.portal;

import com.bbcloud.common.Const;
import com.bbcloud.common.ServerResponse;
import com.bbcloud.pojo.User;
import com.bbcloud.service.IFileService;
import com.bbcloud.service.IUserService;
import com.bbcloud.util.PropertiesUtil;
import com.bbcloud.vo.UserVo;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * Created by liuzhu on 2018/11/2.
 */
@Controller
@RequestMapping("/user/")
public class UserController {

    @Autowired
    private IUserService iUserService;
    @Autowired
    private IFileService iFileService;

    /**
     * 用户登录
     * @param account  邮箱
     * @param password  密码
     * @return  用户对象和token
     */
    @RequestMapping(value = "login.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<UserVo> login(String account, String password, HttpSession session){
        ServerResponse<UserVo>response = iUserService.login(account,password);
        if (response.isSuccess()){
                session.setAttribute(Const.CURRENT_USER,response.getData());
        }
        return  response;
    }

    /**
     * 注册新用户
     * @param account  用户名
     * @param validCode 验证码
     * @param password  密码
     * @return
     */
    @RequestMapping(value = "reigster.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse register(String account,String validCode,String password,String invitationCode){
        return iUserService.register(account,validCode,password,invitationCode);
    }


    /**
     * 获取验证码
     * @param account 邮箱
     * @return
     */
    @RequestMapping(value = "validcode.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse getValidCode(String account){
        return iUserService.getValidCode(account);
    }

    /**
     * 获取用户信息
     * @return
     */
    @RequestMapping(value = "get_user_info.do",method= RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> getUserInfo(String account, String token){
        return iUserService.getUserInfo(account,token);
    }

    /**
     * 重置密码-忘记密码
     * @param account
     * @param password
     * @param validCode
     * @return
     */
    @RequestMapping(value = "forget_reset_password.do",method= RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> forgetResetPassword(String account,String password,String validCode){
        return iUserService.forgetResetPassword(account,password,validCode);
    }

    @RequestMapping("upload.do")
    @ResponseBody
    public ServerResponse  uploadImage(@RequestParam(value = "upload_file",required = false) MultipartFile file, String userId,HttpServletRequest request){

        String path = request.getSession().getServletContext().getRealPath("upload");
//        String targetFileName = iFileService.upload(file,path);
//        String url = PropertiesUtil.getProperty("ftp.server.http.prefix")+targetFileName;
//
//        Map fileMap = Maps.newHashMap();
//        fileMap.put("uri",targetFileName);
//        fileMap.put("url",url);
//        return ServerResponse.createBySuccess(fileMap);
        return iUserService.uploadHeadImage(file,path,userId);
    }

    /**
     * 判断用户是否登录
     * @return
     */
    @RequestMapping("islogin.do")
    @ResponseBody
    public ServerResponse isLogin(String account,String token){
        return iUserService.isLogin(account,token);
    }


    /**
     * 方便测试使用的Web端上传图片接口
     * @param file
     * @param request
     * @return
     */
    @RequestMapping("temp_up_image_interface.do")
    @ResponseBody
    public ServerResponse tempUpImageInterface(@RequestParam(value = "upload_file",required = false) MultipartFile file, HttpServletRequest request){
        String path = request.getSession().getServletContext().getRealPath("upload");
        String targetFileName = iFileService.upload(file,path);
        String url = PropertiesUtil.getProperty("ftp.server.http.prefix")+targetFileName;
        Map fileMap = Maps.newHashMap();
        fileMap.put("uri",targetFileName);
        fileMap.put("url",url);
        return ServerResponse.createBySuccess(fileMap);
    }

}





























